package com.walk.zworld.ui.activity

import android.content.Context
import android.content.Intent
import android.view.View
import android.widget.EditText
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import com.base.lib.base.utils.ToastUtil
import com.base.lib.base.utils.ext.view.showToast
import com.base.lib.base.view.BaseMvvmActivity
import com.base.lib.common.utils.SoftInputUtil
import com.walk.zworld.R
import com.walk.zworld.api.MainAPI
import com.walk.zworld.api.bean.UserBean
import com.walk.zworld.api.http.BaseHttpCallBack
import com.walk.zworld.api.http.CommonAppConfig
import com.walk.zworld.api.http.HttpCallback
import com.walk.zworld.api.http.ParseHttpCallback
import com.walk.zworld.mvvm.viewModel.LoginActivityViewModel
import com.walk.zworld.ui.MainActivity

class LoginActivity : BaseMvvmActivity<LoginActivityViewModel>() {

    override fun onBindViewModel(): Class<LoginActivityViewModel>  = LoginActivityViewModel::class.java

    override fun onBindLayout(): Int  = R.layout.activity_login

    companion object {
        fun start(context: Context) {
            context.startActivity(Intent(context, LoginActivity::class.java))
        }
    }

    override fun initData() {
//        setTop("登录")
        findViewById<View>(R.id.btn_login).setOnClickListener { login() }
        findViewById<View>(R.id.tv_register).setOnClickListener { startActivity(Intent(this, RegisterActivity::class.java)) }
    }

    override fun initView() {}

    private fun login() {
        val loginId = findViewById<EditText>(R.id.et_login_id)
        SoftInputUtil.hideSoftInput(this, loginId)
        val enterpriseName = loginId.text.toString().trim()
        val password = findViewById<EditText>(R.id.et_login_password).text.toString().trim()
        when {
            enterpriseName == "" -> "账号不能为空".showToast()
            password == "" -> "密码不能为空".showToast()
            else -> {
            }
        }
//        mViewModel.login(enterpriseName, password).observe(this) {
//            mmkv.encode(Constant.IS_LOGIN, true)
//            LiveEventBus.get(Constant.IS_LOGIN).post(true)
//            finish()
//        }
        MainAPI.loginByCode(enterpriseName, password, object : ParseHttpCallback<JSONObject?>() {
            override fun onSuccess(code: Int, msg: String?, info: JSONObject?) {
                if (isSuccess(code)) {
                    ToastUtil.showToast("登陆成功")
                    if (info != null) {
                        loginingSucc(info)
                    }
                } else {
                    if (msg != null) {
                        ToastUtil.showToast(msg)
                    }
                }
            }
        })
    }

    private fun loginingSucc(jsonObject: JSONObject) {
        val token = jsonObject.getString("token")
        CommonAppConfig.setLoginInfo("0", token, true)
        askUserInfo()
    }

    private fun askUserInfo() {
        MainAPI.getBaseInfo(object : HttpCallback() {
            override fun onSuccess(code: Int, msg: String?, info: Array<String?>) {
                if (BaseHttpCallBack.isSuccess(code) && info.size > 0) {
                    val json = info[0]
                    val userBean: UserBean = JSON.parseObject(json, UserBean::class.java)
                    userBean.setIsVip(JSON.parseObject(json).getIntValue("isVip") == 1)
                    userBean.isVip = userBean.isVip
                    CommonAppConfig.setUserBean(userBean, json)
                    startActivity(Intent(baseContext, MainActivity::class.java))
                    finish()
                }
            }
        })
    }
}